package com.dagger.weapon.service.impl;

import com.dagger.weapon.dao.ArticleContentsMapper;
import com.dagger.weapon.entity.ArticleContents;
import com.dagger.weapon.entity.SearchArticle;
import com.dagger.weapon.service.ArticleContentService;
import com.dagger.weapon.utils.PageHelper;
import com.dagger.weapon.vo.ResStatus;
import com.dagger.weapon.vo.ResultVO;
import tk.mybatis.mapper.entity.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleContentServiceImpl implements ArticleContentService {

    @Autowired
    private ArticleContentsMapper articleContentsMapper;

    @Override
    public ResultVO addArticleContent(ArticleContents goal) {
        int i = articleContentsMapper.insertUseGeneratedKeys(goal);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success",goal);
        }else {
            return new ResultVO(ResStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVO searchArticles(SearchArticle params) {
        //1.查询总记录数
        Example example = new Example(ArticleContents.class);
        Example.Criteria criteria = example.createCriteria();
        String status = params.getStatus();
        if(status != null && !"".equals(status)){
            criteria.andLike("status", status);
        }
        Integer channelId = params.getChannelId();
        if(channelId != null){
            criteria.andLike("channelId", String.valueOf(channelId));
        }
        Date minDate = params.getMinDate();
        if(minDate != null ){
            criteria.andGreaterThan("artCreate",minDate);
        }
        Date maxDate = params.getMaxDate();
        if(maxDate != null){
            criteria.andLessThan("artCreate",maxDate);
        }
        int count = articleContentsMapper.selectCountByExample(example);


        Integer Limit = params.getLimit();
        Integer Start = params.getPageStart();
        if(Limit != null && Start != null){
            //2.计算总页数
            int pageCount = count%Limit==0?count/Limit:count/Limit+1;
            //3.计算当页第一条
            params.setPageStart((Start-1)*Limit);
            //4.分页查询
            List<ArticleContents> articles = articleContentsMapper.searchArticles(params);
            PageHelper<ArticleContents> artPageHelper = new PageHelper<>(count, pageCount, articles);
            return new ResultVO(ResStatus.OK,"SUCCESS",artPageHelper);
        }else {
            params.setLimit(10);
            params.setPageStart(0);
            int pageCount = 1;
            List<ArticleContents> articles = articleContentsMapper.searchArticles(params);
            PageHelper<ArticleContents> artPageHelper = new PageHelper<>(count, pageCount, articles);
            return new ResultVO(ResStatus.OK,"SUCCESS",artPageHelper);
        }

    }

    @Override
    public ResultVO deleteArticle(String aid) {
        //删除指定aid的文章
        int i = articleContentsMapper.deleteByPrimaryKey(aid);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success","第"+aid+"条文章-已成功被删除");
        }else {
            return new ResultVO(ResStatus.NO,"fail","第"+aid+"条文章-不存在，再看看");
        }
    }

    @Override
    public ResultVO onlyArticle(String aid) {
        //查询指定aid的文章
        ArticleContents articleContent = articleContentsMapper.selectByPrimaryKey(aid);
        if(articleContent != null){
            return new ResultVO(ResStatus.OK,"success",articleContent);
        }else {
            return new ResultVO(ResStatus.NO,"fail","第"+aid+"条文章-不存在,再想想");
        }
    }

    @Override
    public ResultVO updateArticle(ArticleContents aim) {
        int i = articleContentsMapper.updateByPrimaryKey(aim);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success",aim);
        }else {
            return new ResultVO(ResStatus.NO,"fail",null);
        }
    }
}
